cd "C:\Users\Juan Andrés Vera\VenAnalytics Dropbox\VenAnalytics\Research\Papers\2022\Effects of Sanctions\ANOVA\Paper\Update 2012-2022"
clear 
local filepath = "`c(pwd)'"

local files : dir "`filepath'" files "*.csv"

tempfile master
save `master', replace empty

foreach x of local files {

	* 2A) Import each file and generate id var (filename without ".csv")
	qui: import delimited "`x'", varnames(1) clear // <-- Change delimiter() if vars are separated by "," or tab

	* 2B) Append each file to masterfile
	append using `master', force
	save `master', replace
}

drop if (tradeflow == "Imports" | tradeflow == "Re-imports" | tradeflow == "Re-exports")
drop classification perioddesc aggregat isleaf tradeflow reporteriso partnercode partner partneriso ndpartnercode ndpartner ndpartneriso customsproccode customs modeoftransportcode modeoftransport commodity qtyunitcode qtyunit qty altqtyunitcode altqtyunit altqty netweightkg grossweightkg ciftradevalueus fobtradevalueus flag tradeflowcode
sort period reportercode tradevalue  commoditycode
quietly by period reportercode tradevalue: gen dup = cond(_N==1,0,_n)
drop if dup > 1
drop dup 
drop if (reportercode == 97 | reportercode == 975)
bysort period reportercode commoditycode (tradevalue): keep if tradevalue==tradevalue[_N]

gen anova = 0 
replace anova = 1 if (commoditycode == 2 | commoditycode == 3 | commoditycode == 4 | commoditycode == 6 | commoditycode == 7 | commoditycode == 8 | commoditycode == 20 | commoditycode == 21 | commoditycode == 22 | commoditycode == 24)
gen anovaextended = 0
replace anovaextended = 1 if (commoditycode == 1 |commoditycode == 2 | commoditycode == 3 | commoditycode == 4 | commoditycode == 7 | commoditycode == 8 | commoditycode == 9 |commoditycode == 10 | commoditycode == 11 | commoditycode == 12 | commoditycode == 15 | commoditycode == 16 |commoditycode == 17 | commoditycode == 18 | commoditycode == 19 | commoditycode == 20 |commoditycode == 21 | commoditycode == 22)
gen baharetal = 0
replace baharetal = 1 if (commoditycode == 2 | commoditycode == 3 | commoditycode == 4 | commoditycode == 6 | commoditycode == 7 | commoditycode == 8 |commoditycode == 10 | commoditycode == 11 | commoditycode == 12 | commoditycode == 13 | commoditycode == 14 | commoditycode == 15 | commoditycode == 16 |commoditycode == 17 | commoditycode == 18 | commoditycode == 19 | commoditycode == 20 |commoditycode == 21 | commoditycode == 22 | commoditycode == 24)
gen clap = 0
replace clap = 1 if (commoditycode == 1 | commoditycode == 2 | commoditycode == 3 | commoditycode == 4 | commoditycode == 5 | commoditycode == 6 | commoditycode == 7 | commoditycode == 8 | commoditycode == 9 | commoditycode == 10 | commoditycode == 11 | commoditycode == 12 | commoditycode == 13 | commoditycode == 14 | commoditycode == 15 | commoditycode == 16 |commoditycode == 17 | commoditycode == 18 | commoditycode == 19 | commoditycode == 20 |commoditycode == 21 | commoditycode == 22)

egen foodanova = total(tradevalue) if (anova == 1), by(period)
egen foodanovaextended = total(tradevalue) if (anovaextended == 1), by(period)
egen foodbaharetal = total(tradevalue) if (baharetal == 1), by(period)
egen foodclap = total(tradevalue) if (clap == 1), by(period)
egen medicines = total(tradevalue) if (commoditycode == 30), by(period)


sort period foodanova foodanovaextended foodbaharetal foodclap medicines
quietly by period foodanova foodanovaextended foodbaharetal foodclap medicines : gen dup = cond(_N==1,0,_n)
drop if dup > 1
drop anova anovaextended baharetal clap dup reporter commoditycode reportercode tradevalue

egen medicines1 = max(medicines), by(period)

sort period
quietly by period:  gen dup = cond(_N==1,0,_n)
drop if dup > 1

drop medicines dup
drop if period == .
rename medicines1 medicines

format foodanova foodanovaextended foodbaharetal medicines foodclap  %12.0f
order period foodanova foodanovaextended foodbaharetal foodclap medicines

gen x = -67 in 1 
replace x = x[_n - 1] + 1 in 2/130

cd ".."
order year period x
save DatasetUpdate2012, replace

	




